//
//  ITToastView.m
//  ITSM
//
//  Created by hehewyp on 15/9/11.
//  Copyright (c) 2015年 itsm. All rights reserved.
//

#import "ITToastView.h"

#define horizenSpace 40

@protocol ITToastViewDelegte <NSObject>


@end

@interface ITToastView()
@property (nonatomic,strong)UITextView *textView;
@property (nonatomic,assign)BOOL isShow;
@end

@implementation ITToastView

- (id)initWithMessage:(NSString *)message
        maskViewAlpha:(CGFloat)alpha
             duration:(CGFloat)time
             delegate:(id)delegate {
    self = [super initWithFrame:CGRectMake(0, 0, kScreen_Width, kScreen_Height)];
    if (self) {
        self.message = message;
        self.maskAlpha = alpha;
        self.time = time;
    }
    return self;
}

- (void)show {
    [self layoutSubviews];
    if ([[[UIDevice currentDevice] systemVersion] floatValue] < 8.0) {
        [[[[UIApplication sharedApplication] delegate] window] addSubview:self];
    } else {
        [[UIApplication sharedApplication].keyWindow addSubview:self];
    }
    
    dispatch_time_t time = (int64_t)(self.time * NSEC_PER_SEC);
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, time), dispatch_get_main_queue(), ^{
        [[self viewWithTag:901] removeObserver:self forKeyPath:@"contentSize"];
        [self hideAnimation];
    });
}

- (void)layoutSubviews {
    [super layoutSubviews];
    UIView *maskView = [[UIView alloc] initWithFrame:self.frame];
    maskView.backgroundColor = [UIColor blackColor];
    maskView.alpha = self.maskAlpha;
    
    
    CGFloat textViewWidth = kScreen_Width - horizenSpace * 2;
    CGFloat textViewHeight = [self.message boundingHeightWithSize:CGSizeMake(textViewWidth, CGFLOAT_MAX) font:[UIFont systemFontOfSize:18.0] lineSpace:8.0] + 10.0;
    if (textViewHeight < 60) {
        textViewHeight = 60;
    }
    
    self.textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, textViewWidth, textViewHeight)];
    self.textView.center = maskView.center;
    self.textView.backgroundColor = RGBColor(75, 79, 82);
    self.textView.textColor = [UIColor whiteColor];
    self.textView.font = [UIFont systemFontOfSize:18.0];
    dispatch_async(dispatch_get_main_queue(), ^{
        self.textView.text = self.message;
    });
    self.textView.textAlignment = NSTextAlignmentCenter;
    self.textView.editable = NO;
    self.textView.selectable = NO;
    self.textView.showsHorizontalScrollIndicator = NO;
    self.textView.showsVerticalScrollIndicator = NO;
    self.textView.layer.cornerRadius = 15.0;
    self.textView.layer.masksToBounds = YES;
    self.textView.tag = 901;
    if (!self.isShow) {
        [self.textView addObserver:self forKeyPath:@"contentSize" options:(NSKeyValueObservingOptionNew) context:NULL];
        [self addSubview:maskView];
        [self addSubview:self.textView];
    }
    self.isShow = YES;
}

// 渐隐动画
- (void)hideAnimation {
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
    animation.duration = 1.2;
    animation.fromValue = [NSNumber numberWithFloat:1.0];
    animation.toValue = [NSNumber numberWithFloat:0.0];
    [self.layer addAnimation:animation forKey:@"opacityAnimation"];
    
    [NSTimer scheduledTimerWithTimeInterval:1.0
                                     target:self
                                   selector:@selector(finishMethod)
                                   userInfo:nil
                                    repeats:NO];
    
}

- (void)finishMethod
{
    [self removeFromSuperview];
}

#pragma mark - KVO

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    UITextView *tv = object;
    // 将textView内容垂直居中
    CGFloat topCorrect = ([tv bounds].size.height - [tv contentSize].height * [tv zoomScale])/2.0;
    topCorrect = ( topCorrect < 0.0 ? 0.0 : topCorrect );
    tv.contentOffset = (CGPoint){.x = 0, .y = -topCorrect};
}

@end
